package com.example.demodb.service;

import com.example.demodb.entity.SysAccount;
import com.example.demodb.repository.ISysAccountRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SysAccountService {
    @Resource
    private ISysAccountRepository sysAccountRepository;


    public List<SysAccount> findAll(){
        return sysAccountRepository.findAll();
    }

    public SysAccount findUserByAccountNameAndPassword(SysAccount sysAccount) {
        return sysAccountRepository.findByAccountNameAndPassword(sysAccount.getAccountName(),sysAccount.getPassword());
    }

    public SysAccount addSysAccount(SysAccount sysAccount) {
        if(sysAccountRepository.findByAccountName(sysAccount.getAccountName()) == null && sysAccountRepository.findByPhone(sysAccount.getPhone()) == null){
            return sysAccountRepository.save(sysAccount);
        }else{
            return null;
        }
    }


    //对于这个·sysAccount，目前系统是没有对内部的字段进行检验是否存在，如果某个字段为空，则会显示null
//    public SysAccount addSysAccount(SysAccount sysAccount) {
//        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
//        Date date = new Date(System.currentTimeMillis());
//        //可以对时间进行进一步处理，也可以不做，反正时间最后都是要麻烦前端进行处理的。
//        sysAccount.setCreateTime(formatter.format(date));
//        return sysAccountRepository.save(sysAccount);
//    }
}
